package com.db.person.controller;

import com.db.person.bean.UserVisit;
import com.db.person.service.IUserVisitService;
import com.db.person.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserVisitController {

    @Autowired
    private IUserVisitService userVisitService;

    @RequestMapping("/uservisit/list")
    public String list(String content,Model model,PageModel pageModel){

        //使用分页插件进行分页操作
        PageHelper.startPage(pageModel.getPageIndex(),pageModel.getPageSize());//第一个参数是当前第几页， 第二个参数每页显示多少条数据
        //1.查询出所有的数据
        List<UserVisit> all = null;

        //为了兼容查询的分页
        if(content!=null && !content.trim().equals("")){
            all = userVisitService.getByContent(content);
            //要把content传递给页面，一个用来显示，一个用来做下次分页再传递
            model.addAttribute("content",content);
        }else{
            all = userVisitService.getAll();
        }

        PageInfo<UserVisit> pageInfo = new PageInfo<>(all);//得到分页相关的信息

        long total = pageInfo.getTotal();//总条数
        pageModel.setRecordCount((int)total);

        List<UserVisit> list = pageInfo.getList();//得到当前页的数据
        model.addAttribute("list",list);//展示的数据
        model.addAttribute("pageModel",pageModel);//前端需要分页的参数
        model.addAttribute("hre","uservisit/list");//分页跳转的路径
        return "uservisit/list";
    }

    @RequestMapping("/uservisit/delete")
    @ResponseBody
    public String delete(Integer id){

        try {
            userVisitService.deleteById(id);
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
            return "no";
        }

    }

    @RequestMapping("/uservisit/deleteAll")
    @ResponseBody
    public String deleteAll(String ids){

        try {
            userVisitService.deleteByIds(ids);
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
            return "no";
        }

    }

}
